package com.alex;

import android.util.Log;
import android.view.View;

import com.bytedance.sdk.openadsdk.TTFeedAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationExpressRenderListener;

import java.util.ArrayList;
import java.util.List;

public class AlexGromoreNativeExpressHandler {

    static final String TAG = AlexGromoreNativeExpressHandler.class.getSimpleName();


    final List<? extends TTFeedAd> expressAdList;
    final List<TTNativeExpressAdWrapper> resultList;

    public AlexGromoreNativeExpressHandler(List<? extends TTFeedAd> expressAdList) {
        this.expressAdList = expressAdList;
        this.resultList = new ArrayList<>();
    }

    public void startRender(final RenderCallback renderCallback) {
        // Native Express
        final int[] size = new int[]{expressAdList.size()};
        for (final TTFeedAd ttExpressFeedAd : expressAdList) {
            ttExpressFeedAd.setExpressRenderListener(new MediationExpressRenderListener() {
                @Override
                public void onRenderFail(View view, String s, int i) {
                    Log.e(TAG, String.format("onRenderFail, errorCode: %d, errorMsg: %s",
                            i, s));
                    size[0] = size[0] - 1;
                    if (size[0] == 0) {
                        if (resultList.size() == 0) {
                            if (renderCallback != null) {
                                renderCallback.onRenderFail(s, i);
                            }
                        } else {
                            if (renderCallback != null) {
                                renderCallback.onRenderSuccess(resultList);
                            }
                            resultList.clear();
                            expressAdList.clear();
                            ;
                        }
                    }
                }

                @Override
                public void onAdClick() {
                }

                @Override
                public void onAdShow() {
                }

                @Override
                public void onRenderSuccess(View view, float v, float v1, boolean b) {
                    Log.i(TAG, "onRenderSuccess()");
                    resultList.add(new TTNativeExpressAdWrapper(ttExpressFeedAd, v, v1));
                    size[0] = size[0] - 1;
                    if (size[0] == 0) {
                        if (renderCallback != null) {
                            renderCallback.onRenderSuccess(resultList);
                        }
                        resultList.clear();
                        expressAdList.clear();
                    }
                }
            });

            ttExpressFeedAd.render();
        }
    }


    public interface RenderCallback {
        void onRenderSuccess(List<TTNativeExpressAdWrapper> ttNativeExpressAds);
        void onRenderFail(String s, int i);
    }

    public class TTNativeExpressAdWrapper {
        public TTFeedAd ttFeedAd;
        public float expressWidth;
        public float expressHeight;

        public TTNativeExpressAdWrapper(TTFeedAd ttFeedAd, float width, float height) {
            this.ttFeedAd = ttFeedAd;
            this.expressWidth = width;
            this.expressHeight = height;
        }
    }
}
